#define quickread
#ifdef quickread
#include <cstdio> 
template <typename T>
inline void read(T& x)
{
    int c=getchar(), f=1; x=0;
    while(c<'0'||'9'<c) {if(c=='-') f=-1; c=getchar();}
    while('0'<=c&&c<='9') 
        x=(x<<3)+(x<<1)+c-'0', c=getchar();
    x*=f;
}
template <typename T>
inline void quickwrite(T x)
{
    if(x<0) putchar('-'), x*=-1;
    if(x>=10) quickwrite(x/10);
    putchar(x%10+'0');
}
#else 
#include <iostream>
#endif
using namespace std;
#define DEBUG
using ll=long long;
ll n, x, cnt1; ll ans;
void init()
{
    read(n), read(x);
    cnt1=__builtin_popcountll(x); //x中有多少个1
}

void solve()
{
    init();
    if(n<=cnt1) ans=x; //直接使用自身拼凑出答案
    else if((n-cnt1)%2==0) ans=x+n-cnt1; //需要添加偶数个，全部添加1
    else{ //需要添加奇数个
        if(x>1) ans=x+n-cnt1+1; //添加n-1个1 1个2
        else if(x==1) ans=n+3; //1 2
        else { //x==0
            if(n==1) ans=-1;
            else ans=n+3;
        }
    }
    quickwrite(ans); puts("");
}

// #undef DEBUG
signed main()
{
    #ifdef DEBUG
        freopen("../in.txt", "r", stdin);
        freopen("../out.txt", "w", stdout);
    #endif
    #ifndef quickread
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    #endif

    int T=1; scanf("%d", &T);
    while(T--) 
    {
        solve();
    }
    return 0;
}
